package com.white.note.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.white.note.AboutActivity;
import com.white.note.LoginActivity;
import com.white.note.MainActivity;
import com.white.note.R;
import com.white.note.UesHelperActivity;
import com.white.note.UpdatePwdActivity;
import com.white.note.base.BaseFragment;
import com.white.note.db.NoteDbHelper;
import com.white.note.entity.UserInfo;

public class MineFragment extends BaseFragment {

    private TextView tv_userName;
    private TextView tv_nickName;

    @Override
    protected int getLayoutResid() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initViews() {
        tv_userName = rootView.findViewById(R.id.tv_userName);
        tv_nickName = rootView.findViewById(R.id.tv_nickName);
    }

    @Override
    protected void setListener() {
        rootView.findViewById(R.id.tv_edit_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
                startActivityForResult(intent, 2000);
            }
        });
        rootView.findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getContext())
                        .setTitle("温馨提示")
                        .setMessage("你确定要清除所有记录吗？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                NoteDbHelper.getInstance(getContext()).clearNote(UserInfo.sUserInfo.getUsername());
                                startActivity(new Intent(getActivity(), MainActivity.class));
                                Toast.makeText(getActivity(), "清理完成~~", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();


            }
        });
        rootView.findViewById(R.id.btn_hepler).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), UesHelperActivity.class));
            }
        });
        rootView.findViewById(R.id.btn_about).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AboutActivity.class));

            }
        });
        rootView.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getContext())
                        .setTitle("温馨提示")
                        .setMessage("你确定要退出吗？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(getActivity(), LoginActivity.class);
                                startActivity(intent);
                                Toast.makeText(getActivity(), "退出登录成功~~", Toast.LENGTH_SHORT).show();
                                getActivity().finish();
                            }
                        })
                        .show();
            }
        });
    }

    @Override
    protected void initData() {
        UserInfo userInfo = UserInfo.getsUserInfo();
        if (null != userInfo) {
            tv_userName.setText(userInfo.getUsername());
            tv_nickName.setText(userInfo.getNickname());
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2000) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        }
    }
}